<?php

use app\modules\Admin\models\storememberModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class storeModel extends Model
{
    use SoftDeletes;

	protected $table 		= 'store';
	protected $primaryKey	= 'id';

    protected $appends = ['status_text', 'service_area_ids_arr', 'images_arr'];

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return string
     */
    public function getStatusTextAttribute()
    {
        $status_text = '禁用';
        if($this->attributes['status']){
            $status_text = '启用';
        }
        return $status_text;
    }

    /**
     * @return array
     */
    public function getOpenweeksTextArrAttribute()
    {
        $openweeks_text_arr = [];
        if(!empty($this->attributes['openweeks'])){
            $openweeks_text_arr = (new \app\modules\Admin\models\storeapplyModel())->getOpenweeksTextArrAttribute($this->attributes['openweeks']);
        }
        return $openweeks_text_arr;
    }

    /**
     * @return array
     */
    public function getOpenweeksArrAttribute()
    {
        $openweeks_arr = [];
        if(!empty($this->attributes['openweeks'])){
            $openweeks_arr = $this->getServiceAreaIdsArrAttribute($this->attributes['openweeks']);
        }
        return $openweeks_arr;
    }

    /**
     * @return array
     */
    public function getImagesArrAttribute()
    {
        $images_arr = [];
        if(!empty($this->attributes['images'])){
            $images_arr = explode(';', $this->attributes['images']);
        }
        return $images_arr;
    }


    /**
     * @param string|null $values
     * @return array
     */
    public function getServiceAreaIdsArrAttribute(string|null $values = ''): array
    {
        $service_area_ids_arr = [];
        if(empty($values)){
            $values = $this->attributes['service_area_ids']?:'';
        }
        if(!empty($values)){
            $ids_arr = explode(',', $values);
            $ids_arr = array_filter($ids_arr, function ($value) {
                if(is_numeric($value)){
                    return intval($value);
                }
            });
            $service_area_ids_arr = array_values($ids_arr);
            unset($ids_arr);
        }
        return $service_area_ids_arr;
    }


    /**
     * @return array
     */
    public function getServiceAreaNamesArrAttribute()
    {
        $service_area_names_arr = [];
        if(!empty($this->attributes['service_area_ids'])){
            $ids_arr = $this->getServiceAreaIdsArrAttribute($this->attributes['service_area_ids']);
            foreach ($ids_arr as $id){
                $area_row = areaModel::query()->where('id', $id)->first()->toArray();
                $area_item = [
                    'id' => intval($id),
                    'name' => areaModel::getFullAreaName(intval($id)),
                    'pid' => $area_row['pid'],
                    'level' => $area_row['level'],
                ];
                $service_area_names_arr[] = json_decode(json_encode($area_item, JSON_UNESCAPED_UNICODE),true);
                unset($area_item);
                unset($area_row);
            }
            unset($id);
            unset($ids_arr);
        }
        return $service_area_names_arr;
    }

    public function members()
    {
        $member_table = (new memberModel())->getTable();
        return $this->hasManyThrough(memberModel::class, storememberModel::class, 'store_id', 'id', 'id', 'member_id')->select(["{$member_table}.id", "{$member_table}.phone", "{$member_table}.nickname", "{$member_table}.realname", "{$member_table}.avatar"]);
    }


}